from MMMM.BuildIndex import sm4_cbc_enc
import hashlib
import hmac



'''text = input("input:")
text = text.lower()
words = text.split()
words = [word.strip('.,!;()[]') for word in words]'''



'''prime = 148300296503541537023967543385571281566188269121696986355794642903163978311488366178620508621203382790644644256351530344017086776119734526929467544594283880389370982620148075975876610977802799762624795305966277480303398775064011730140292446255046328316204433780624821532022148316432675132662755400581378870583
key_s = b'\x1a\xde\x9b\xda\xadNq\xf4\xa5\x04g\x02\x99E\x8d\xcf[\x0f\xa6Dx\xe7\t\xbb\x1e\xb2n\xbd|h\xcf\x16'
key_m_1 = b'\xba\x8a\xed\x17y\x8f2[y]{\xd7\x1a\xb8\x05\x08\xf2CL\xe8\xdaY\xa2\xd0o=\x87\xc5De\xb1\x95'
key_m_2= '8bjp6lyrgfn370i5'
key_1 = b'\xaa%\x00"\x08)+\x84\xf1I\x0e\xeb\xb6\xb2\xd4L\x8eBd\xbc{\xc1\xc7\xc9\xd3`\xb1\xfb\xbbZ\xa2\xed'
iv = 'gvmr10z4pc6xqn3l'
iv_bytes = bytes(iv,encoding='utf8')'''


def get_token(words):
    prime = 148300296503541537023967543385571281566188269121696986355794642903163978311488366178620508621203382790644644256351530344017086776119734526929467544594283880389370982620148075975876610977802799762624795305966277480303398775064011730140292446255046328316204433780624821532022148316432675132662755400581378870583
    key_s = b'\x1a\xde\x9b\xda\xadNq\xf4\xa5\x04g\x02\x99E\x8d\xcf[\x0f\xa6Dx\xe7\t\xbb\x1e\xb2n\xbd|h\xcf\x16'
    key_m_1 = b'\xba\x8a\xed\x17y\x8f2[y]{\xd7\x1a\xb8\x05\x08\xf2CL\xe8\xdaY\xa2\xd0o=\x87\xc5De\xb1\x95'
    key_m_2 = '8bjp6lyrgfn370i5'
    key_1 = b'\xaa%\x00"\x08)+\x84\xf1I\x0e\xeb\xb6\xb2\xd4L\x8eBd\xbc{\xc1\xc7\xc9\xd3`\xb1\xfb\xbbZ\xa2\xed'
    iv = 'gvmr10z4pc6xqn3l'
    iv_bytes = bytes(iv, encoding='utf8')
    T = []
    for i in range(0, len(words)):
        T.append([])
    for i in  range(0,len(words)):
        e = sm4_cbc_enc(words[i].encode('utf-8'), iv_bytes, key_m_2.encode('utf-8'))
        msk = hmac.new(key_1,words[i].encode('utf-8'),digestmod=hashlib.sha256).hexdigest()
        ci = hmac.new(key_m_1,words[i].encode('utf-8'),digestmod=hashlib.sha256).hexdigest()

        tj_1 =hmac.new(key_s,str(int.from_bytes(e,'little')^int(msk.upper(),16)^int(ci.upper(),16)).encode('utf-8'),digestmod=hashlib.sha256).hexdigest()
        tj_1 = int(tj_1.upper(),16)

        tj_2 =int.from_bytes(e,'little') * int(ci.upper(),16)
        tj_2 = tj_2 % prime

        tj_3 =int.from_bytes(e,'little') * int(msk.upper(),16)
        tj_3 =tj_3 % prime

        T[i].append(tj_1)
        T[i].append(tj_2)
        T[i].append(tj_3)
    return T

'''T = get_token(words)
print("-----------token---------")
for i in T:
    print(i)'''